import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/widgets/gt_dashed_line_text.dart';
import 'package:lib_uikit/widgets/text/gt_text.dart';
import 'package:lottie/lottie.dart';
import 'package:easy_debounce/easy_throttle.dart';

//large52高度，medium40高度，small30高度，xsmall24，xxsmall20;
// text类型有两个专属size：text14，text12，分别对应14sp文字，和12sp文字
enum GTButtonSizeV3 { large, medium, small, xsmall, xxsmall, text14, text12 }

//outlined规范只有30和24高度
enum GTButtonTypeV3 {
  primary,
  secondary,
  thirty,
  red_button,
  green_button,
  white_button,
  text_black,
  text_blue,
  outlined,
  outlined_white_text
}

class GTButtonV3 extends StatefulWidget {
  final Widget? startImage;
  final Widget? endImage;
  final String text;

  //宽度是否充满父容器
  final bool? isFullWidth;

  //button尺寸，默认small32高度
  final GTButtonSizeV3? buttonSize;

  //button类型，默认primary
  final GTButtonTypeV3? buttonType;

  //左边icon
  final IconData? startIconFont;

  //右边icon
  final IconData? endIconFont;

  //是否可用，默认true
  final bool? isEnabled;

  //是否带有虚线下划线（只有text类型有下划线），默认false
  final bool? isDashed;

  //是否展示loading样式
  final bool? isShowLoading;
  final VoidCallback? onPressed;

  //如果外层有二次测量的需求，需要把autoSize设置为false，
  //否则会报错：LayoutBuilder does not support returning intrinsic dimensions.
  final bool? autoSize;
  final double? width;
  final double?paddingHorizontal;

  const GTButtonV3({super.key,
    required this.text,
    this.startImage,
    this.endImage,
    this.isFullWidth,
    this.startIconFont,
    this.endIconFont,
    this.isEnabled,
    this.isDashed,
    this.onPressed,
    this.isShowLoading,
    this.buttonSize = GTButtonSizeV3.small,
    this.buttonType = GTButtonTypeV3.primary,
    this.autoSize = true,
    this.width,
    this.paddingHorizontal});

  @override
  State<StatefulWidget> createState() => _ButtonStateV3();
}

class _ButtonStateV3 extends State<GTButtonV3> {
  bool _isFullWidth = false;
  bool isDown = false;
  bool _isEnabled = true;
  bool _isDashed = false;

  @override
  Widget build(BuildContext context) {
    _isFullWidth = widget.isFullWidth ?? false;
    _isEnabled = widget.isEnabled ?? true;
    _isDashed = widget.isDashed ?? false;

    GTButtonSizeV3 buttonSize = widget.buttonSize ?? GTButtonSizeV3.small;
    TextStyle textStyle;
    double buttonHeight = 0;
    double buttonRadius = 20;
    double iconMargin;
    double paddingHorizontal;
    double iconSize;
    double textSize;
    String loadingRes = 'packages/lib_uikit/assets/json/gt_btn_loading_white_v3.json';
    /*
    按钮不同类型样式设置
     */
    GTButtonTypeV3 buttonType = widget.buttonType ?? GTButtonTypeV3.primary;
    Color backgroundColor;
    Color clickedColor;
    Color textColor;
    Color textClickedColor;
    Color borderColor = Colors.transparent;
    Color disableBackgroundColor = GTColorsV3.neutral1.color;
    switch (buttonType) {
      case GTButtonTypeV3.primary:
        backgroundColor = GTColorsV3.brand1.color;
        clickedColor = GTColorsV3.brand2.color;
        textColor = GTColorsV3.text6.color;
        textClickedColor = GTColorsV3.text6.color;
        break;
      case GTButtonTypeV3.secondary:
        backgroundColor = GTColorsV3.neutral1.color;
        clickedColor = GTColorsV3.neutral3.color;
        textColor = GTColorsV3.text1.color;
        textClickedColor = GTColorsV3.text1.color;
        loadingRes =
        'packages/lib_uikit/assets/json/gt_btn_loading_black_v3.json';
        break;
      case GTButtonTypeV3.thirty:
        backgroundColor = GTColorsV3.neutral3.color;
        clickedColor = GTColorsV3.neutral1.color;
        textColor = GTColorsV3.text1.color;
        textClickedColor = GTColorsV3.text5.color;
        loadingRes =
        'packages/lib_uikit/assets/json/gt_btn_loading_black_v3.json';
        break;
      case GTButtonTypeV3.red_button:
        backgroundColor = GTColorsV3.pd1.color;
        clickedColor = GTColorsV3.pd5.color;
        textColor = GTColorsV3.text6.color;
        textClickedColor = GTColorsV3.text6.color;
        break;
      case GTButtonTypeV3.green_button:
        backgroundColor = GTColorsV3.pd2.color;
        clickedColor = GTColorsV3.pd6.color;
        textColor = GTColorsV3.text6.color;
        textClickedColor = GTColorsV3.text6.color;
        break;
      case GTButtonTypeV3.white_button:
        backgroundColor = GTColorsV3.cmpt1.color;
        clickedColor = GTColorsV3.brand1.color;
        textColor = GTColorsV3.text9.color;
        textClickedColor = GTColorsV3.text6.color;
        loadingRes =
        'packages/lib_uikit/assets/json/gt_btn_loading_black_v3.json';
        break;
      case GTButtonTypeV3.text_black:
        backgroundColor = Colors.transparent;
        clickedColor = Colors.transparent;
        textColor = GTColorsV3.text1.color;
        textClickedColor = GTColorsV3.text5.color;
        paddingHorizontal = 0;
        disableBackgroundColor = Colors.transparent;
        break;
      case GTButtonTypeV3.text_blue:
        backgroundColor = Colors.transparent;
        clickedColor = Colors.transparent;
        textColor = GTColorsV3.text5.color;
        textClickedColor = GTColorsV3.brand2.color;
        disableBackgroundColor = Colors.transparent;
        paddingHorizontal = 0;
        break;
      case GTButtonTypeV3.outlined:
        clickedColor = GTColorsV3.neutral3.color;
        textColor = GTColorsV3.text1.color;
        textClickedColor = GTColorsV3.text1.color;
        loadingRes =
        'packages/lib_uikit/assets/json/gt_btn_loading_black_v3.json';
        backgroundColor = Colors.transparent;
        borderColor = GTColorsV3.divider5.color;
        disableBackgroundColor = Colors.transparent;
        break;

      case GTButtonTypeV3.outlined_white_text:
        clickedColor = GTColorsV3.neutral3.color;
        textColor = GTColorsV3.text6.color;
        textClickedColor = GTColorsV3.text6.color;
        loadingRes =
        'packages/lib_uikit/assets/json/gt_btn_loading_black_v3.json';
        backgroundColor = Colors.transparent;
        borderColor = Colors.transparent;
        disableBackgroundColor = Colors.transparent;
        break;
    }

    /*
    按钮不同尺寸样式设置
     */
    switch (buttonSize) {
      case GTButtonSizeV3.large:
        buttonHeight = 52;
        iconMargin = 8;
        paddingHorizontal = 16;
        iconSize = 16;
        textSize = 16;
        textStyle = gtTextStyleW500();
        buttonRadius = 52;
        break;
      case GTButtonSizeV3.medium:
        buttonHeight = 40;
        iconMargin = 8;
        paddingHorizontal = 16;
        iconSize = 16;
        textSize = 14;
        textStyle = gtTextStyleW500();
        break;
      case GTButtonSizeV3.small:
        buttonHeight = 30;
        iconMargin = 8;
        paddingHorizontal = 12;
        iconSize = 14;
        textSize = 14;
        textStyle = gtTextStyleW500();
        break;
      case GTButtonSizeV3.xsmall:
        buttonHeight = 24;
        iconMargin = 4;
        paddingHorizontal = 8;
        iconSize = 12;
        textSize = 12;
        textStyle = gtTextStyleW500();
        break;
      case GTButtonSizeV3.xxsmall:
        buttonHeight = 20;
        iconMargin = 4;
        paddingHorizontal = 6;
        iconSize = 12;
        textSize = 12;
        textStyle = gtTextStyleW400();
        break;
      case GTButtonSizeV3.text14:
        iconMargin = 2;
        iconSize = 12;
        textSize = 14;
        paddingHorizontal = 0;
        textStyle = gtTextStyleW500();
        break;
      case GTButtonSizeV3.text12:
        iconMargin = 2;
        iconSize = 12;
        textSize = 12;
        paddingHorizontal = 0;
        textStyle = gtTextStyleW400();
        break;
    }

    Widget dashedText() {
      return GTDashedLineText(
        text: widget.text,
        textStyle: textStyle.copyWith(
            color: _isEnabled
                ? isDown
                ? textClickedColor
                : textColor
                : GTColorsV3.text7.color,
            fontSize: textSize),
        onDashedTextClick: null,
        showDashedLine: _isDashed,
      );
    }

    Widget _text() {
      if (widget.autoSize == false) {
        if (_isDashed) {
          return Container(
            height: textSize == 14 ? 16 : 14,
            child: dashedText(),
          );
        }
        return Text(
          widget.text,
          style: textStyle.copyWith(
              color: _isEnabled
                  ? isDown
                  ? textClickedColor
                  : textColor
                  : GTColorsV3.text7.color,
              fontSize: textSize, height: 1),
        );
      }
      if (_isDashed) {
        return dashedText();
      }
      return AutoSizeText(
        widget.text,
        minFontSize: 12,
        maxLines: 1,
        maxFontSize: textSize,
        style: textStyle.copyWith(
            color: _isEnabled
                ? isDown
                ? textClickedColor
                : textColor
                : GTColorsV3.text7.color,
            fontSize: textSize, height: 1),
      );
    }

    return Material(
      color: Colors.transparent,
      child: GestureDetector(
        onTap: widget.onPressed != null
            ? () {
          if (_isEnabled) {
            EasyThrottle.throttle(
                "uikitButtonClick", const Duration(milliseconds: 500), () {
              widget.onPressed?.call();
            });
          }
        }
            : null,
        onTapDown: widget.onPressed != null
            ? (_) {
          setState(() {
            if (_isEnabled) {
              isDown = true;
            }
          });
        }
            : null,
        onTapUp: widget.onPressed != null
            ? (_) {
          setState(() {
            isDown = false;
          });
        }
            : null,
        onTapCancel: widget.onPressed != null
            ? () {
          setState(() {
            isDown = false;
          });
        }
            : null,
        child: Container(
            width: widget.width,
            alignment: _isFullWidth ? Alignment.center : null,
            height: buttonHeight > 0 ? buttonHeight : null,
            padding: EdgeInsets.symmetric(
                horizontal: widget.paddingHorizontal ?? paddingHorizontal),
            decoration: BoxDecoration(
              color: _isEnabled
                  ? isDown
                  ? clickedColor
                  : backgroundColor
                  : disableBackgroundColor,
              borderRadius: BorderRadius.circular(buttonRadius),
              border: Border.all(
                color: borderColor,
                width: 1,
              ),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                if (widget.startImage != null &&
                    !(widget.isShowLoading ?? false)) ...[
                  widget.startImage!,
                  SizedBox(width: iconMargin),
                ],
                if (widget.startIconFont != null &&
                    !(widget.isShowLoading ?? false)) ...[
                  Icon(
                    widget.startIconFont,
                    size: iconSize,
                    color: _isEnabled
                        ? isDown
                        ? textClickedColor
                        : textColor
                        : GTColorsV3.text7.color,
                  ),
                  SizedBox(width: iconMargin),
                ],
                if (widget.isShowLoading ?? false) ...[
                  SizedBox(
                    width: 24,
                    child: Lottie.asset(
                      loadingRes,
                      repeat: true,
                      animate: true,
                    ),
                  ),
                ],
                Visibility(
                  visible: !(widget.isShowLoading ?? false),
                  child: Flexible(
                    child: _text(),
                  ),
                ),
                if (widget.endImage != null &&
                    !(widget.isShowLoading ?? false)) ...[
                  SizedBox(width: iconMargin),
                  widget.endImage!,
                ],
                if (widget.endIconFont != null &&
                    !(widget.isShowLoading ?? false)) ...[
                  SizedBox(width: iconMargin),
                  Icon(
                    widget.endIconFont,
                    size: iconSize,
                    color: _isEnabled
                        ? isDown
                        ? textClickedColor
                        : textColor
                        : GTColorsV3.icon7.color,
                  ),
                ],
              ],
            )),
      ),
    );
  }
}
